package gui;

import gui.CellIcon.State;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import animation.AnimationFrame;

public class TablePanel extends JPanel
{
	
	private static final long serialVersionUID = 6185666812172861829L;
	
	private CellIcon[][] cells;
	
	private AnimationFrame currFrame;
	
	/**
	 * dimension of this table in number of cells
	 */
	int x, y;
	
	public TablePanel(int x, int y)
	{
		this.x = x;
		this.y = y;
		
		//create the initial frame to modify
		this.currFrame = new AnimationFrame();
		
		//each cell is 50, leave 5 pixels on each side 
		// w = 8 * 55 + 5 
		this.setSize(x * 55 + 5, y * 55 + 5);
		this.setBackground(Color.BLACK);
		this.setLayout(null);
	
		this.initCells();
		
	}

	private void initCells() 
	{
		this.cells = new CellIcon[x][y];
		
		for(int j = 0; j < y; j++)
		{
			for(int i = 0; i < x; i++)
			{
				CellIcon c = new CellIcon(i, j);
				this.cells[i][j] = c;
				this.add(c);
			}
		}
	}
	
	/**
	 * Clears all cells in the panel to be DARK
	 */
	public void clearAll()
	{
		for(int j = 0; j < y; j++)
		{
			for(int i = 0; i < x; i++)
			{
				this.cells[i][j].setState(State.DARK);
			}
		}
	}
	
	//TODO load frame
	
	/**
	 * @param phantom, if true, the current frame is made "phantom" on the table
	 * @return the current frame in the display, and resets all.
	 */
	public AnimationFrame grabFrame(boolean phantom)
	{
		AnimationFrame retFrame = new AnimationFrame();
		
		for(int j = 0; j < retFrame.getHeight(); j++)
		{
			for(int i = 0; i < retFrame.getWidth(); i++)
			{
				boolean selected = this.cells[i][j].getIsSelected();
				retFrame.cells[i][j] = selected;
				
				// make phantom
				this.cells[i][j].setState(selected && phantom ? State.PHANTOM : State.DARK);
			}
		}
		
		return retFrame;		
	}
	
	
	
	
}
